var shuju = 1;
$(document).ready(function() {
	findAllDeptType();

	$("#leaderChooseWindow").dialog({
				autoOpen : false,
				minWidth : 520,
				modal : true,
				title : '请选择任务的指派部门',
				close : function() {
					return false;
				}
			});

	// 确定选择人员
	$("#selOk").click(function() {
				var leader = $("#selDepts option:selected");

				if (leader.text() == "") {
					$("#leaderChooseWindow").dialog('close');
					return false;
				} else {
					var str = "";
					$("#selDepts option:selected").each(function() {
								str += $(this).text() + ",";
							});
					$("#taskDept").val(str);
					$("#leaderChooseWindow").dialog('close');
				}

			});
	// 取消选择人员
	$("#selCancel").click(function() {
				$("#leaderChooseWindow").dialog('close');
			});

	$("#chosedept").click(function() {
				$("#leaderChooseWindow").dialog("open");
			});


});

/**
 * 添加人员结点——方法
 * 
 * @param users
 * @param ulId
 * @return
 */
function setPeople(users, deptId, ulId) {
	var idOfUl = "#" + $.trim(ulId);
	$.each(users, function(i, n) {
				shuju++;
				ckBoxId = "ckbox" + shuju;
				$(idOfUl).append("<li>" + "<input type=\"checkbox\" id=\""
						+ ckBoxId + "\"/>" + "<label for=" + ckBoxId + ">"
						+ n.UName + "</label>" + "</li>");
				var ckBox_id = "#" + $.trim(ckBoxId);
				$(ckBox_id).click(function() {
					var uid = n.UId;
					if ($(ckBox_id).attr("checked") == true) {// 如果是打钩的话，增加到右边的select
						var isOnly = true;
						$("#selDepts option:selected").each(function(i, n) {
									if ($(this).attr("name") == uid) {
										isOnly = false;
									}
								});
						if (isOnly) {
							setTimeout(function() {
										$("#selDepts")
												.append("<option class='class1' name="
														+ n.UId
														+ " selected value='"
														+ deptId
														+ "'>"
														+ n.UName + "</option>");
									}, 100);
						}
					} else {// 取消打钩，清楚右边的框
						$("#selDepts option").each(function(i, n) {
									if ($(this).attr("name") == uid) {
										$(this).remove();
									}
								});
					}
				});
			});
}

function chgIcon(spanId) {
	var span_id = "#" + $.trim(spanId);
	var imgsrc = $(span_id).children("img").attr("src");
	if (imgsrc == "/ag/wzp/images/jiahao.gif") {
		$(span_id).children("img").attr("src", "/ag/wzp/images/jianhao.gif");
	} else {
		$(span_id).children("img").attr("src", "/ag/wzp/images/jiahao.gif");
	}
}

// 获取所有部门类型
function findAllDeptType() {
	var extern = 0; // 为了在循环注册ID时，使各个DOM（span,ul） 'S ID不一样。
	$.ajax({
		type : "post",
		url : "/ag/lzw/getTaskDept_findAllDeptType.action",
		success : function(data) {
			$("#deptType").empty();
			$("<option value='0'>请选择</option>").appendTo("#deptType");
			$("#q_UDept").append("<option value='0'>请选择</option>");
			$("#deptul").empty();
			$.each(data.deptTypes, function(i, n) {
				$("<option value=" + n.dtId + ">" + n.dtName + "</option>")
						.appendTo("#deptType");
				// below motified by czy
				extern++;
				var temp = "span_id" + extern;
				var temp2 = "ul_id" + extern;
				$("#deptul").append("<li>"
						+ "<span style=\"cursor: pointer;\" id=" + temp + ">"
						+ "<img src=\"/ag/wzp/images/jiahao.gif\">" + n.dtName
						+ "</span>" + "<ul style=\"list-style:none\" id="
						+ temp2 + ">" + "</ul>" + "</li>");
				var span_id = "#" + $.trim(temp);
				var ul_id = "#" + $.trim(temp2);
				$(span_id).children("img").attr("src",
						"/ag/wzp/images/jiahao.gif");
				$(span_id).click(function() {
					// 每点击一次改变一次图标
					chgIcon(temp);
					var dtId = n.dtId;
					$.ajax({
						type : "post",
						data : {"dtId" : dtId},
						url : "/ag/lzw/getTaskDept_findUserDeptByDtId.action",
						success : function(data) {
							if ($(ul_id).text() != "") {
								$(ul_id).empty();
							} else {
								// alert(data.errMsg);
								if (data.errMsg == "0") { // 指挥长
									$.each(data.deptPeoples, function(i, n) {
										extern++;
										var temp3 = "span2_id" + extern;
										var temp4 = "ul2_id" + extern;
										var temp5 = $.trim(n.deptID);
										$(ul_id).append("<li><span style=\"cursor: pointer;\" id="
														+ temp3
														+ ">"
														+ "<img src=\"/ag/wzp/images/jiahao.gif\">"
														+ "</span>"
														+ "<input type=\"checkbox\" style=\"cursor: pointer;\" id=\""
														+ temp5
														+ "\"/>"
														+ "<label style=\"cursor: pointer;\" for="
														+ temp5
														+ ">"
														+ n.name
														+ "</label>"
														+ "<ul style=\"list-style:none\" id="
														+ temp4
														+ ">"
														+ "</ul>"
														+ "</li>");
										var ckBox_id = "#" + temp5;
										// checkbox打钩选择部门
										$(ckBox_id).click(function() {
											var did = n.deptID;
											if ($(ckBox_id).attr("checked") == true) {// 如果是打钩的话，增加到右边的select
												var isOnly = true;
												$("#selDepts option:selected").each(function(i, n) {
															if ($(this).attr("value") == did) {
																isOnly = false;
															}
														});
												if (isOnly) {
													setTimeout(function() {
														$("#selDepts").append("<option class='class1' name="
																		+ n.id
																		+ " selected value='"
																		+ n.deptID
																		+ "'>"
																		+ n.name
																		+ "</option>");
													}, 100);
												}
											} else {// 取消打钩，清楚右边的框
												$("#selDepts option").each(
														function(i, n) {
															if ($(this).attr("value") == did) {
																$(this).remove();
															}
														});
											}
										});

										var span2_id = "#" + $.trim(temp3);
										var ul2_id = "#" + $.trim(temp4);
										$(span2_id).children("img").attr("src","/ag/wzp/images/jiahao.gif");

										$(span2_id).click(function() {
											chgIcon(temp3);
											var udid = $(this).next().attr("id");
											if (udid == "DI0100000000") {
												var udid_1 = n.deptID;
												$.ajax({
													type : "post",
													data : {"Did" : udid_1},
													url : "/ag/lzw/getTaskDept_findDeptAndPeoples.action",
													success : function(data) {
														if ($(ul2_id).text() != "") { // 右边的选择框有选项时
															$(ul2_id).empty();
															$("#selDepts").empty();
														} else {
															if (data.users != null) {
																setPeople(data.users,n.deptID,temp4);
																// tmpe4为ul的id
															}
															$.each(data.depts,function(i,n) {
																		extern++;
																		var temp5 = "span3_id"+ extern;
																		var temp6 = "ul3_id"+ extern;
																		$(ul2_id).append("<li>"
																						+ "<span style=\"cursor: pointer;\" id="
																						+ temp5
																						+ ">"
																						+ "<img src=\"/ag/wzp/images/jiahao.gif\">"
																						+ n.udName
																						+ "</span>"
																						+ "<ul style=\"list-style:none\" id="
																						+ temp6
																						+ ">"
																						+ "</ul>"
																						+ "</li>");
																		var span3_id = "#"+ $.trim(temp5);
																		var ul3_id = "#"+ $.trim(temp6);
																		$(span3_id).children("img").attr("src","/ag/wzp/images/jiahao.gif");
																		$(span3_id).click(function() {
																		                    chgIcon(temp5);
																							var udid_2 = n.udId;
																							$.ajax({
																									type : "post",
																									data : {"Did" : udid_2},
																									url : "/ag/lzw/getTaskDept_findDeptAndPeoples.action",
																									success : function(data) {
																													if ($(ul3_id).text() != "") {
																														$(ul3_id).empty();
																														if (data.users != null) { 
																															// 加入返回的数据里的users信息不为空
																															$.each(data.users,function(i,n) { 
																																// 遍历返回的users信息
																																		var uid = n.UId;
																																		// alert(uid);
																																		$("#selDepts option").each(function(i,n) { 
																																			// 遍历右边已选的信息
																																				if ($(this).attr("name") == uid) {
																																				// alert("remove");
																																				// 若有匹配则删除
																																				$(this).remove();
																																				}
																																		});
																															});
																														} else {
																															$("#selDepts").empty();
																														}
																													} else {
																														if (data.depts != null) {
																														}
																														if (data.users != null) {
																															setPeople(data.users,n.udId,temp6);
																															// tmpe6为ul的id
																														}
																													}
																												}
																											});

																						});
																	});

														}

													}

												});
											}/* else {
												
												var imgsrc = $(this).children("img").attr("src");
												var did = n.deptID;
												var isOnly = true;
												$("#selDepts option:selected")
														.each(function(i, n) {
															if ($(this)
																	.attr("value") == did) {
																isOnly = false;
															}
														});
												if (isOnly) {
													var strHtml = "<option class='deptClass' name='"
															+ n.id
															+ "' selected value='"
															+ n.deptID
															+ "'>"
															+ n.name
															+ "</option>";
													$("#selDepts")
															.append(strHtml);
												}
											}*/
										});

									});
								} else {
									$.each(data.deptPeoples, function(i, n) {
										extern++;
										var temp3 = "span2_id" + extern;
										var temp4 = "ul2_id" + extern;
										$(ul_id)
												.append("<li>"
														+ "<span style=\"cursor: pointer;\" id="
														+ temp3
														+ ">"
														+ "<img src=\"/ag/wzp/images/jiahao.gif\">"
														+ n.name
														+ "</span>"
														+ "<ul style=\"list-style:none\" id="
														+ temp4 + ">" + "</ul>"
														+ "</li>");
										var span2_id = "#" + $.trim(temp3);
										var ul2_id = "#" + $.trim(temp4);
										$(span2_id).children("img").attr("src",
												"/ag/wzp/images/jiahao.gif");
										$(span2_id).click(function() {
											// 每点击一次改变一次图标
											var imgsrc = $(this)
													.children("img")
													.attr("src");

											var UDept = n.deptID;

											$.ajax({
												type : "post",
												url : "/ag/lzw/getTaskDept_findDeptPeople.action",
												data : {
													"Did" : UDept
												},
												success : function(data) {
													if (data.peoples == null)
														alert("该部门中没有直属人员。");
													else {
														// 每点击一次改变一次图标
														if (imgsrc == "/ag/wzp/images/jiahao.gif") {
															$(span2_id)
																	.children("img")
																	.attr(
																			"src",
																			"/ag/wzp/images/jianhao.gif");
														} else {
															$(span2_id)
																	.children("img")
																	.attr(
																			"src",
																			"/ag/wzp/images/jiahao.gif");
														}
													}
													if ($(ul2_id).text() != "") {
														$(ul2_id).empty();
													} else {
														$.each(data.peoples,
																function(i, n) {
																	extern++;
																	var temp5 = "span3_id"
																			+ extern;
																	$(ul2_id)
																			.append("<li>"
																					+ "<span style=\"cursor: pointer;\" id="
																					+ temp5
																					+ ">"
																					+ "<img src=\"/ag/wzp/images/people.gif\">"
																					+ n.name
																					+ "</span>"
																					+ "</li>");
																	var span3_id = "#"
																			+ $
																					.trim(temp5);
																	$(span3_id)
																			.children("img")
																			.attr(
																					"src",
																					"/ag/wzp/images/jiahao.gif");
																	$(span3_id)
																			.click(
																					function() {
																						var uid = n.id;
																						var isOnly = true;
																						$("#selDepts option:selected")
																								.each(
																										function(
																												i,
																												n) {
																											if ($(this)
																													.attr("name") == uid) {
																												isOnly = false;
																											}
																										});

																						if (isOnly) {
																							var strHtml = "<option class='class1' name='"
																									+ n.id
																									+ "' selected value='"
																									+ n.deptID
																									+ "'>"
																									+ n.name
																									+ "</option>";
																							setTimeout(
																									function() {
																										$("#selDepts")
																												.append(strHtml);
																									},
																									500);
																						}
																					});

																});
													}
												}
											}); // ajax
										}); // $(span2_id).click
									});
								}

							}
						}
					});
				});

			});
		}
	});
}
